package com.liang.pcc.excel.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.liang.pcc.domain.TopologyDataDTO;
import com.liang.pcc.excel.BaseExcelListener;
import com.liang.pcc.excel.model.TopologyData;
import com.liang.pcc.service.TopologyDataService;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * 拓扑数据监听器
 */
@Slf4j
public class TopologyDataListener extends BaseExcelListener<TopologyData> {

    private final TopologyDataService topologyDataService;
    private final Long fileId;
    private final Long userId;

    private final List<TopologyDataDTO> dataList = new ArrayList<>();

    public TopologyDataListener(TopologyDataService topologyDataService, CountDownLatch latch, Long fileId, Long userId) {
        super(latch);
        this.topologyDataService = topologyDataService;
        this.fileId = fileId;
        this.userId = userId;
    }

    @Override
    public void invoke(TopologyData rowData, AnalysisContext analysisContext) {
        TopologyDataDTO topologyData = TopologyDataDTO.builder()
                .name(rowData.getNameOfBusLineEquipment())
                .parentName(rowData.getParent())
                .fileId(fileId)
                .userId(userId)
                .build();
        dataList.add(topologyData);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        topologyDataService.saveBatch(dataList);
        super.doAfterAllAnalysed(analysisContext);
        log.info("Sheet4数据插入完成！");
    }
}
